package main

import (
	"fmt"
	"time"
)

/*===标准库_time===

 */
func main() {
	timeDemo()
	timestampDemo()
	timestampDemo2(1600310908)

}

//时间类型
func timeDemo() {
	now := time.Now() //获取当前 时间对象 (日历形式时间、包含时区信息)
	fmt.Printf("current time:%v\n", now)
	year := now.Year()
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	fmt.Printf("时间类型:\n %d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
	date, m, d := now.Date() //年、月、日
	fmt.Printf("年月日:\n date:%d ,m:%d ,d:%d", date, m, d)
}
func timestampDemo() {
	now := time.Now()            //获取当前时间
	timestamp1 := now.Unix()     //时间戳
	timestamp2 := now.UnixNano() //纳秒时间戳
	fmt.Println()
	fmt.Printf("获取当前时间戳:\n current timestamp1:%v\n", timestamp1)
	fmt.Printf("获取当前时间戳(纳秒级):\n current timestamp2:%v\n", timestamp2)
}

//时间戳(自1970年1月1日（08:00:00GMT）至当前时间的总毫秒数)
func timestampDemo2(timestamp int64) {
	timeObj := time.Unix(timestamp, 0) //将时间戳转为时间格式
	fmt.Println("转换时间戳：", timeObj)
	year := timeObj.Year()     //年
	month := timeObj.Month()   //月
	day := timeObj.Day()       //日
	hour := timeObj.Hour()     //小时
	minute := timeObj.Minute() //分钟
	second := timeObj.Second() //秒
	fmt.Printf("时间戳:\n %d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}
